package com.gongyibao.gbase.mq.topic;

/**
 * @Author: 张启峰
 * @Date: 2023/4/17 14:22
 * @Description: gyb-
 */

import com.cvicse.inforsuitemq.InforsuiteMQConnection;
import com.cvicse.inforsuitemq.InforsuiteMQConnectionFactory;

import javax.jms.*;

public class Topic_Publish {
    String topicname = "VirtualTopic.aa0530"; // 主题名
    boolean persistent = true;// 是否持久化消息
    int priporty = 4; // 消息的优先级
    int timeToLive = 0; // 消息生命周期
    private boolean transacted = false;//是否是事务消息
    private int ackMode = Session.AUTO_ACKNOWLEDGE;//消息确认机制，AUTO_ACKNOWLEDGE为自动确认,CLIENT_ACKNOWLEDGE为手动确认
    int messageCount = 100;// 发送消息数
    String body = "测试虚拟消息主题aa";//消息体内容
    String url = "failover:(tcp://192.168.6.68:8688)";

    Connection connection = null;
    Session session;
    Destination destination;
    MessageProducer producer;

    public static void main(String[] args) {
        System.out.println("Starting example now...");
        Topic_Publish Publish = new Topic_Publish();
        try {
            Publish.sendMessage();
            Publish.close();
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Finished running the example Producer app.");
        System.out.print("\n\n\n");
    }

    public void sendMessage() throws JMSException {
        InforsuiteMQConnectionFactory connectionFactory = new InforsuiteMQConnectionFactory(
                InforsuiteMQConnection.DEFAULT_USER,
                InforsuiteMQConnection.DEFAULT_PASSWORD, url);
        connection = connectionFactory.createConnection();
        session = connection.createSession(transacted, ackMode);
        // 主题
        destination = session.createTopic(topicname);
        // 创建生产者
        producer = session.createProducer(destination);
        // 设置是否进行消息持久化
        if (persistent) {
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } else {
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
        // 设置消息的有效期，默认为0，表示永不过期
        if (timeToLive != 0) {
            producer.setTimeToLive(timeToLive);
        }
        // 消息优先级，从0-9十个级别，0-4是普通消息，5-9是加急消息，默认4
        if (priporty != 4) {
            producer.setPriority(priporty);
        }
        //TextMessage message = session.createTextMessage(body);
        // 发送消息
        for (int i = 0; i < messageCount; i++) {
            TextMessage message = session.createTextMessage(body+i);
            producer.send(message);
            TextMessage txtMsg = (TextMessage) message;
            String msg = txtMsg.getText();
            System.out.println("Sending message:" + message);
        }
        if (transacted) {
            session.commit();//事务消息则执行提交
        } else if (ackMode == Session.CLIENT_ACKNOWLEDGE) {
            //message.acknowledge();
        }
    }

    public void close() throws JMSException {
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}